package com.leyou.cart.controller;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart){

        cartService.addCart(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("list")
    public ResponseEntity<List<Cart>> listCart(){

        return ResponseEntity.ok(cartService.listCart());
    }

    @PostMapping("list")
    public ResponseEntity<Void> mergeCarts(@RequestBody List<Cart> carts){
        this.cartService.mergeCarts(carts);
        return ResponseEntity.ok().build();

    }

    @PutMapping
    public ResponseEntity<Void> updateCart(
            @RequestParam("id")Long id,
            @RequestParam("num")Integer num){
        cartService.updateCart(id, num);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId")Long skuId){

        cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();
    }
}
